/* global wx, shoproot, parseFloat */

/**
 * 订单信息查看
 * @description Hope You Do Good But Not Evil
 * @copyright   Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      Chenyong Cai <ycchen@iwshop.cn>
 * @package     Wshop
 * @link        http://www.iwshop.cn
 */

require(['config'], function (config) {

    require(['util', 'jquery', 'Spinner'], function (util, $,Spinner) {

        // 加载快递配送信息
        if ($('#expresscode').val() !== '' && $('#express-dt').length > 0) {

            Spinner.spin($("#loading-wrap").get(0), 200);

            $.post('?/Order/ajaxGetExpressDetails', {
                com: $('#expresscom').val(),
                nu: $('#expresscode').val()
            }, function (res) {
                var json= eval("("+res+")");
                var html=jsonFormat(json);
                $('#express-dt').html(html);
                $('#loading-wrap').remove();
                Spinner.stop();
            });
        }
        $(".exp-code").each(function () {
            if ($(this).attr("code") !== '') {
                $this=$(this);
                Spinner.spin($(this).parent().next().find(".loading-wrap").get(0), 200);

                $.post('?/Order/ajaxGetExpressDetails', {
                    com: $(this).attr("com"),
                    nu: $(this).attr("code")
                }, function (res) {
                    var json= eval("("+res+")");
                    var html=jsonFormat(json);

                    $this.parent().next().find(".express-dt").html(html);
                    $this.parent().next().find(".loading-wrap").remove();
                    Spinner.stop();
                });
            }
        })
        $("#stop").click(function(){
            var id=$(this).attr("data-id");
            if(window.confirm('你确定要暂停本次吗？')){
                $.ajax({
                    url: "?/Uc/stopPk",
                    data:"id="+id,
                    type:"post",
                    dataType:"json",
                    success: function(res){
                       if(res.status){
                           alert("暂停成功!");
                           window.location.reload();
                       }else {
                           alert(res.msg);
                       }
                    }
                });
                return true;
            }else{

                return false;
            }


            })
        function jsonFormat(json){
            console.log(json);
            var html="";

            var status="";
            if(json.error_code!=0){

                status=json.reason;
                html+="<li>"+status+"</li>";
            }else{

                $.each(json.result.list,function(i,v){
                    html+="<li>"+v.datetime+"&nbsp;&nbsp;"+v.remark+"</li>";
                })
            }



            return html;
        }
        $("#edit_address").click(function(){
            var id=$(this).attr("data-id");
             addAddressCallback(id,0);

        })
        $("#edit_all_address").click(function(){
            var id=$(this).attr("data-id");


             addAddressCallback(id,1);

        })
        function addAddressCallback(id,all) {
            wx.openAddress({
                success: function (res) {
                    var userName = res.userName; // 收货人姓名
                    var postalCode = res.postalCode; // 邮编
                    var provinceName = res.provinceName; // 国标收货地址第一级地址（省）
                    var cityName = res.cityName; // 国标收货地址第二级地址（市）
                    var countryName = res.countryName; // 国标收货地址第三级地址（国家）
                    var detailInfo = res.detailInfo; // 详细收货地址信息
                    var nationalCode = res.nationalCode; // 收货地址国家码
                    var telNumber = res.telNumber; // 收货人手机号码

                    var Address = res.provinceName + res.cityName + res.countryName + res.detailInfo;
                    alert("id="+id+"&all="+all+"&userName="+userName+"&telNumber="+telNumber+"&address="+Address);
                    $.ajax({
                        url: "?/Uc/changeAddressPk",
                        data:"id="+id+"&all="+all+"&userName="+userName+"&telNumber="+telNumber+"&address="+Address,
                        type:"post",
                        dataType:"json",
                        success: function(res){
                            if(res.status){
                                alert("修改成功!");
                                window.location.reload();
                            }else {
                                alert(res.msg);
                            }
                        }
                    });

                },
                fail:function(){
                    $('#wrp-btn').html('授权失败');
                }
            });

        }

    });

});